<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>预约管理 - 管理后台</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="/css/style.css">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark navbar-custom">
        <div class="container-fluid">
            <a class="navbar-brand" href="/admin/index.html">
                <i class="bi bi-shield-check"></i> 管理后台
            </a>
            <div class="ms-auto">
                <span class="navbar-text text-white me-3">
                    <i class="bi bi-person-circle"></i> <span id="adminName"></span>
                </span>
                <a href="#" class="btn btn-outline-light btn-sm" id="logoutBtn">
                    <i class="bi bi-box-arrow-right"></i> 退出
                </a>
            </div>
        </div>
    </nav>

    <div class="container-fluid">
        <div class="row">
            <div class="col-md-2 sidebar">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link" href="/admin/index.html">
                            <i class="bi bi-speedometer2"></i> 数据统计
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/admin/seats.html">
                            <i class="bi bi-grid-3x3"></i> 座位管理
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/admin/bookings.html">
                            <i class="bi bi-calendar-check"></i> 预约管理
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/admin/violations.html">
                            <i class="bi bi-exclamation-triangle"></i> 违约管理
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/admin/users.html">
                            <i class="bi bi-people"></i> 用户管理
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/admin/config.html">
                            <i class="bi bi-gear"></i> 系统配置
                        </a>
                    </li>
                </ul>
            </div>

            <div class="col-md-10 p-4">
                <h4 class="page-header"><i class="bi bi-calendar-check"></i> 预约管理</h4>

                <div class="card">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-hover table-sm">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>用户</th>
                                        <th>座位</th>
                                        <th>日期</th>
                                        <th>时段</th>
                                        <th>签到时间</th>
                                        <th>签退时间</th>
                                        <th>状态</th>
                                    </tr>
                                </thead>
                                <tbody id="bookingList"></tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://unpkg.com/layui@2.8.18/dist/layui.js"></script>
    <script src="/js/request.js"></script>
    <script>
        var layer;
        layui.use('layer', function(){
            layer = layui.layer;
        });
    </script>
    <script>
        $(document).ready(function() {
            if (!request.checkLogin()) return;
            const user = request.getUserInfo();
            if (user.role !== 'ADMIN') {
                window.location.href = '/index.html';
                return;
            }
            $('#adminName').text(user.name);
            loadBookings();
            $('#logoutBtn').click(function(e) {
                e.preventDefault();
                request.logout();
            });
        });

        function loadBookings() {
            request.get('/api/admin/booking/list').then(data => {
                let html = '';
                data.slice(0, 100).forEach(booking => {
                    const statusInfo = getStatusInfo(booking.status);
                    html += `
                        <tr>
                            <td>${booking.id}</td>
                            <td>${booking.userId}</td>
                            <td><span class="badge bg-primary">${booking.seatId}</span></td>
                            <td>${booking.bookingDate}</td>
                            <td>${booking.timeSlot}</td>
                            <td>${formatTime(booking.checkInTime)}</td>
                            <td>${formatTime(booking.checkOutTime)}</td>
                            <td><span class="badge ${statusInfo.class}">${statusInfo.text}</span></td>
                        </tr>
                    `;
                });
                $('#bookingList').html(html);
            });
        }

        function getStatusInfo(status) {
            const map = {
                'WAIT_CHECKIN': {text: '待签到', class: 'bg-warning'},
                'IN_USE': {text: '使用中', class: 'bg-success'},
                'COMPLETED': {text: '已完成', class: 'bg-info'},
                'CANCELED': {text: '已取消', class: 'bg-secondary'},
                'RELEASED': {text: '已释放', class: 'bg-danger'}
            };
            return map[status] || {text: status, class: 'bg-secondary'};
        }

        function formatTime(time) {
            if (!time) return '-';
            return time.replace('T', ' ').substring(0, 16);
        }
    </script>
</body>
</html>

